package com.luo.service;

import com.luo.mapper.RoleMapper;
import com.luo.pojo.Role;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService {

    @Resource
    private RoleMapper roleMapper;

    public Map<String,Object> selectRoleList(String roleName,
                                                   String roleLevel,
                                                   int page,
                                                   int limit){
        int index = (page - 1) * limit;// 根据前端传递的页码和每页显示多少条，计算出当前页起始的数据下标值
        Map<String,Object> map = new HashMap<>();
        // 根据分页完成职位数据查询
        List<Role> roles = roleMapper.selectRoleList(roleName,roleLevel,index,limit);
        // 统计所有职位个数
        int count = roleMapper.selectRoleCount(roleName,roleLevel);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",roles);
        return map;
    }

    public Map<String,Object> insert(Role role){
        Map<String,Object> map = new HashMap<>();
        roleMapper.insert(role);
        map.put("code",200);
        map.put("msg","新增职位成功");
        map.put("data",role);
        return map;
    }

    public Map<String,Object> update(Role role){
        Map<String,Object> map = new HashMap<>();
        roleMapper.update(role);
        map.put("code",200);
        map.put("msg","更新职位成功");
        map.put("data",role);
        return map;
    }

    public Map<String,Object> delete(Integer id){
        Map<String,Object> map = new HashMap<>();
        roleMapper.delete(id);
        map.put("code",200);
        map.put("msg","删除职位成功");
        map.put("id",id);
        return map;
    }

    public Map<String,Object> selectRoleNames(){
        Map<String,Object> map = new HashMap<>();
        List<Role> roles = roleMapper.selectRoleNames();
        map.put("code",0);
        map.put("msg","");
        map.put("data",roles);
        return map;
    }
}
